package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.TestCourse;
import com.ruoyi.system.service.TestCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/course")
public class TestCourseControlller extends BaseController {
    @Autowired
    private TestCourseService courseService;
    @PreAuthorize("@ss.hasPermi('system:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(TestCourse course)
    {
        //分页查询，PageHelper.startPage
        startPage();
        //将查询到的数据封装到list
        List<TestCourse> list = courseService.selectCourseList(course);
        //封装数据
        return getDataTable(list);
    }

    /*条件查询，在修改操作之前*/
    @PreAuthorize("@ss.hasPermi('system:course:query')")
    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable Long courseId)
    {
        return success(courseService.selectCourseById(courseId));
    }

    /*增加操作
     * @RequestBody将请求报文转换为对象，
     * @Validated参数校验，后端需要验证参数
     * */
    @PreAuthorize("@ss.hasPermi('system:course:add')")
    @Log(title = "管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TestCourse course)
    {
        //校验岗位是否唯一
        if (!courseService.checkCourseNameUnique(course))
        {
            return error("新增'" + course.getCourseName() + "'失败，名称已存在");
        }
        else if (!courseService.checkCourseCodeUnique(course))
        {
            return error("新增'" + course.getCourseCode() + "'失败，编码已存在");
        }
        course.setCreateBy(getUsername());
        return toAjax(courseService.insertCourse(course));
    }

    /*修改操作*/
    @PreAuthorize("@ss.hasPermi('system:course:edit')")
    @Log(title = "管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TestCourse course)
    {
        if (!courseService.checkCourseNameUnique(course))
        {
            return error("修改'" + course.getCourseName()+ "'失败，名称已存在");
        }
        else if (!courseService.checkCourseCodeUnique(course))
        {
            return error("修改'" + course.getCourseCode() + "'失败，编码已存在");
        }
        course.setUpdateBy(getUsername());
        return toAjax(courseService.updateCourse(course));
    }


    /*删除操作，前端传递id来进行操作*/
    @PreAuthorize("@ss.hasPermi('system:course:remove')")
    @Log(title = "管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{courseIds}")
    public AjaxResult remove(@PathVariable Long[] courseIds)
    {
        return toAjax(courseService.deleteCourseByIds(courseIds));
    }
}
